package com.yanqu.road.dao.impl.config;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.config.skill.SkillInfo;
import com.yanqu.road.entity.config.skill.SkillUpgradeInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SkillDaoImpl extends TempDao {

	public Map<Integer, SkillInfo> getSkillInfoMap() {
		Map<Integer, SkillInfo> dataMap = new HashMap<>();
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_p_skill";
		try {
			getDb().setConnName(DbNameStrategy.getConfigDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				SkillInfo data = new SkillInfo();
				data.setId(rs.getInt("id"));
				data.setType(rs.getInt("type"));
				data.setParamList(StringUtils.stringToIntegerList(rs.getString("params"), "\\|"));
				data.setUpgradeParamList(StringUtils.stringToIntegerList(rs.getString("upgradeParams"), "\\|"));
				data.setMaxLevel(rs.getInt("maxLevel"));
				String upgradeItem = rs.getString("upgradeItem");
				String[] split = upgradeItem.split("\\|");
				List<Integer> list = new ArrayList<>();
				for (String s : split) {
					list.add(Integer.valueOf(s));
				}
				data.setConsumeGoodsId(list);
				String upgradeType = rs.getString("upgradeType");
				 split = upgradeType.split("\\|");
				list = new ArrayList<>();
				for (String s : split) {
					list.add(Integer.valueOf(s));
				}
				data.setUpgradeType(list);
				data.setStars(rs.getInt("stars"));
				dataMap.put(data.getId(), data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}

		return dataMap;
	}

	public Map<Integer, Map<Integer, SkillUpgradeInfo>> getSkillUpgradeInfoMap() {
		Map<Integer, Map<Integer, SkillUpgradeInfo>> dataMap = new HashMap<>();
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_p_skill_upgrade";
		try {
			getDb().setConnName(DbNameStrategy.getConfigDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				SkillUpgradeInfo data = new SkillUpgradeInfo();
				data.setConsumeCount(rs.getLong("consume"));
				data.setLevel(rs.getInt("level"));
				data.setUpgradeType(rs.getInt("upgradeType"));
				if (!dataMap.containsKey(data.getUpgradeType())) {
					dataMap.put(data.getUpgradeType(), new HashMap<>());
				}
				dataMap.get(data.getUpgradeType()).put(data.getLevel(), data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}
}
